package com.cyh.pangu.ds;

public class MyCircleQueue {

    private int[] array;

    private int front;

    private int rear;

    public MyCircleQueue(int capacity) {
        array = new int[capacity + 1];
    }

    public void enQueue(int value) throws Exception {
//        System.out.println("front" + front);
        if ((rear + 1) % array.length  == front) {
            throw new Exception("队列已满");
        }
        array[rear] =  value;
        rear = (rear + 1) % array.length;
        System.out.println("rear:" + rear);
    }



    public int deQueue() throws Exception {
        System.out.println("rear:" + rear + ",front:" + front);
        if (rear == front) {
            throw new Exception("为空");
        }
        int outElement = array[front];
        array[front] = 0 ;
        front = (front + 1) % array.length;
        return outElement;
    }

    public void output() {
        System.out.println("prepared  output-----------");
        for (int i = front; i != rear; i = (i + 1) % array.length) {

            System.out.println(i + ":" + array[i]);
        }
    }

    public static void main(String[] args) throws Exception {
        MyCircleQueue circleQueue = new MyCircleQueue(4);
        circleQueue.enQueue(1);
        circleQueue.enQueue(2);
        circleQueue.enQueue(3);
        circleQueue.enQueue(4);
//        circleQueue.deQueue();
//        circleQueue.deQueue();
//        circleQueue.deQueue();
//        circleQueue.deQueue();
        circleQueue.output();
    }
}
